/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.tothgya.timetable.elvira;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author tothgya
 */
public class Journey extends hu.tothgya.timetable.Journey {

    String url;

    public Journey(String start, String destination, String url, String text, Calendar date) {
        try {
            this.url = url;
            String[] times = text.split("-|\\ ");

            this.start = start;
            this.destination = destination;

            this.leave = Calendar.getInstance();
            this.arrive = Calendar.getInstance();

            Calendar c1 = (Calendar) date.clone();
            Calendar c2 = (Calendar) date.clone();

            boolean leavedateok = false;
            boolean arrivedateok = false;
            int i = 0;
            do {
                leavedateok = true;
                try {
                    c1.setTime(new SimpleDateFormat("kk:mm").parse(times[i]));
                    i++;
                } catch (ParseException pe) {
                    this.start += times[i];
                    leavedateok = false;
                    i++;
                }
            } while (!leavedateok);
            do {
                arrivedateok = true;
                try {
                    c2.setTime(new SimpleDateFormat("kk:mm").parse(times[i]));
                    i++;
                } catch (ParseException pe) {
                    this.destination += times[i];
                    arrivedateok = false;
                    i++;
                }
            } while (!arrivedateok);

            leave.set(Calendar.HOUR_OF_DAY, c1.get(Calendar.HOUR_OF_DAY));
            leave.set(Calendar.MINUTE, c1.get(Calendar.MINUTE));

            arrive.set(Calendar.HOUR_OF_DAY, c2.get(Calendar.HOUR_OF_DAY));
            arrive.set(Calendar.MINUTE, c2.get(Calendar.MINUTE));

        } catch (ArrayIndexOutOfBoundsException ex) {
            Logger.getLogger(Journey.class.getName()).log(Level.SEVERE, null, ex);
        }
        ConnectionFactory cf = new ConnectionFactory(url, date);
        connections = cf.getConnections();
    }
}
